<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    
    @register="registerDrawer"
    @ok="handleSubmit"
    width="40%"
  >
  <!-- :okAuth="'sys:area:edit'" -->
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsSysAreaForm">
  import { ref, unref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { updatePoCodeBatch } from '/@/api/wms/btdm/btdmBoxMain';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('btdm.btdmBoxMain');
  const { showMessage } = useMessage();
  const record = ref<any>({});
  const { meta } = unref(router.currentRoute);
  const getTitle = computed(() => ({
    icon: meta.icon || 'ant-design:book-outlined',
    value: t('批量修改'),
  }));

  const inputFormSchemas: FormSchema[] = [
    
    {
      label: t('采购单号'),
      field: 'newPoCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      // required: true,
    },
    {
      label: t('装箱条码'),
      field: 'hid.boxCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('物料代码'),
      field: 'hid.addCode',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
   
  ];

  const [registerForm, { resetFields, setFieldsValue, updateSchema, validate,getFieldsValue }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    console.log(data);
    
    record.value = data
    // const res = await areaForm(data);
    // record.value = (res.area || {}) as Area;
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      let data = await validate();
      setDrawerProps({ confirmLoading: true });
      let params = {
        "newPoCode":data.newPoCode,
        "hid.boxCode":data.hid.boxCode,
        "hid.addCode":data.hid.addCode,
        ...record.value,
      }
      // console.log('submit', params, data, record);
      const res = await updatePoCodeBatch(params);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('common.validateError'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
